﻿namespace Smart.Utils.UI
{
    using DevExpress.XtraEditors;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Button)), Designer(typeof(ImageButtonDesigner))]
    public class ImageButton : PictureEdit
    {
        private Dictionary<ButtonState, Image> mImages = new Dictionary<ButtonState, Image>();
        private ButtonState mState = ButtonState.Normal;

        public ImageButton()
        {
            this.DoubleBuffered = true;
            base.ResizeRedraw = true;
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
        }

        private void DrawImage(Graphics g, Image image)
        {
            Rectangle clientRectangle = base.ClientRectangle;
            this.Image = image;
            g.DrawImage(image, clientRectangle);
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if (!base.Enabled)
            {
                this.mState = ButtonState.Disabled;
                base.Invalidate();
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.mState = ButtonState.Pushed;
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.mState = ButtonState.Normal;
            base.Invalidate();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if ((e.Button != MouseButtons.Left) && (this.mState != ButtonState.Hover))
            {
                this.mState = ButtonState.Hover;
                base.Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            this.mState = ButtonState.Normal;
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            if (this.mImages.ContainsKey(this.mState))
            {
                Image image = this.mImages[this.mState];
                if (image != null)
                {
                    this.DrawImage(g, image);
                }
            }
        }

        public void PerformClicked()
        {
            base.OnClick(EventArgs.Empty);
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(70, 0x17);
            }
        }

        [DefaultValue((string) null)]
        public Image DisabledImage
        {
            get
            {
                if (!this.mImages.ContainsKey(ButtonState.Disabled))
                {
                    this.mImages.Add(ButtonState.Disabled, null);
                }
                return this.mImages[ButtonState.Disabled];
            }
            set
            {
                this.mImages[ButtonState.Disabled] = value;
                base.Invalidate();
            }
        }

        [DefaultValue((string) null)]
        public Image HoverImage
        {
            get
            {
                if (!this.mImages.ContainsKey(ButtonState.Hover))
                {
                    this.mImages.Add(ButtonState.Hover, null);
                }
                return this.mImages[ButtonState.Hover];
            }
            set
            {
                this.mImages[ButtonState.Hover] = value;
                base.Invalidate();
            }
        }

        [DefaultValue((string) null)]
        public Image NormalImage
        {
            get
            {
                if (!this.mImages.ContainsKey(ButtonState.Normal))
                {
                    this.mImages.Add(ButtonState.Normal, null);
                }
                return this.mImages[ButtonState.Normal];
            }
            set
            {
                this.mImages[ButtonState.Normal] = value;
                base.Invalidate();
            }
        }

        [DefaultValue((string) null)]
        public Image PushedImage
        {
            get
            {
                if (!this.mImages.ContainsKey(ButtonState.Pushed))
                {
                    this.mImages.Add(ButtonState.Pushed, null);
                }
                return this.mImages[ButtonState.Pushed];
            }
            set
            {
                this.mImages[ButtonState.Pushed] = value;
                base.Invalidate();
            }
        }

        private enum ButtonState
        {
            Normal,
            Hover,
            Pushed,
            Disabled
        }
    }
}

